//
//  RSSystemUtit.m
//  RSToolNSObject
//
//  Created by thinkjoy on 16/10/26.
//  Copyright © 2016年 Thinkjoy_RS. All rights reserved.
//

#import "RSSystemUtit.h"

#import <MessageUI/MessageUI.h>

#pragma mark - 日志开关
#ifdef DEBUG
#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define DLog(...)
#endif


@implementation RSSystemUtit

#pragma mark    - 打电话
+ (void)callPhoneNum:(NSString *)phoneNum
{
    /*
     *  个人感觉方法一更快一点
     */
    
    //    if (phoneNum.length <= 0) {
    //        DLog(@"拨打号码为空");
    //        return;
    //    }
    
    NSURL * url =   [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phoneNum]];
    
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        
        //方法一
        if ([[[UIDevice currentDevice] systemVersion] floatValue]<10) {
            DLog(@"小于IOS10");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
            [[UIApplication sharedApplication] openURL:url];
#pragma clang diagnostic pop
            
        }else{
            DLog(@"不小于IOS10");
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
                //iOS10 openURL 新特性 http://blog.csdn.net/hurricane_eye/article/details/52673232
            }];
        }
        
        //        //方法二
        //        UIWebView * callWebview = [[UIWebView alloc]init];
        //        [callWebview loadRequest:[NSURLRequest requestWithURL:url]];
        //        [[UIApplication sharedApplication].keyWindow addSubview:callWebview];
        
    }else{
        DLog(@"您的设备不支持通话功能");
        UIAlertController * alertC  =   [UIAlertController alertControllerWithTitle:@"提示" message:@"当前设备不支持通话功能" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * action  =   [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil];
        [alertC addAction:action];
        dispatch_async(dispatch_get_main_queue(), ^{
            [[[UIApplication sharedApplication].keyWindow rootViewController] presentViewController:alertC animated:YES completion:nil];
        });
    }
}

#pragma mark    - 发送短信
+ (void)sendSMSToPhoneNum:(NSString *)phoneNum
{
    if (phoneNum.length <= 0) {
        DLog(@"号码为空");
        return;
    }
    
    NSURL * url =   [NSURL URLWithString:[NSString stringWithFormat:@"sms://%@",phoneNum]];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        
        if ([[[UIDevice currentDevice] systemVersion] floatValue]<10) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
            [[UIApplication sharedApplication] openURL:url];
#pragma clang diagnostic pop
        }else{
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        }
        
    }else{
        UIAlertController * alertC  =   [UIAlertController alertControllerWithTitle:@"提示" message:@"当前设备不支持发送短信" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * action  =   [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil];
        [alertC addAction:action];
        dispatch_async(dispatch_get_main_queue(), ^{
            [[[UIApplication sharedApplication].keyWindow rootViewController] presentViewController:alertC animated:YES completion:nil];
        });
    }
}
#pragma mark    - 发送邮件
+ (void)sendE_MailWithRecipients:(NSArray <NSString*>*)recipients ccRecipients:(NSArray <NSString*>*)ccRecipients bccRecipients:(NSArray <NSString*>*)bccRecipients subject:(NSString *)subject body:(NSString *)body
{
    NSMutableString * urlStr    =   [NSMutableString stringWithString:@"mailto:"];
    
    //添加收件人
    if ([recipients count]) {
        [urlStr appendFormat:@"%@",[recipients componentsJoinedByString:@","]];
    }
    
    [urlStr appendString:@"?"];
    
    //添加抄送
    if ([ccRecipients count]) {
        [urlStr appendFormat:@"cc=%@",[ccRecipients componentsJoinedByString:@","]];
    }
    
    //添加密送
    if ([bccRecipients count]) {
        [urlStr appendFormat:@"&bcc=%@",[bccRecipients componentsJoinedByString:@","]];
    }
    
    //添加主题
    if ([subject length]) {
        [urlStr appendFormat:@"&subject=%@",subject];
    }
    
    //添加邮件内容
    if ([body length]) {
        [urlStr appendFormat:@"&body=%@",body];
    }
    
    NSURL * url =   [NSURL URLWithString: [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        if ([[[UIDevice currentDevice] systemVersion] floatValue]<10) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
            [[UIApplication sharedApplication] openURL:url];
#pragma clang diagnostic pop
        }else{
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        }
    }else{
        UIAlertController * alertC  =   [UIAlertController alertControllerWithTitle:@"提示" message:@"当前设备不支持发送邮件" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * action  =   [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil];
        [alertC addAction:action];
        dispatch_async(dispatch_get_main_queue(), ^{
            [[[UIApplication sharedApplication].keyWindow rootViewController] presentViewController:alertC animated:YES completion:nil];
        });
    }
}

#pragma mark    - 打开对应的设置界面

+ (void)jumpToSettingPageByRSPrefsRoot:(RSPrefsRoot)prefsRoot
{
    NSURL * url =   nil;

    if ([[[UIDevice currentDevice] systemVersion] floatValue]<10) {
        switch (prefsRoot) {
                case RSPrefsRoot_Setting:
                    url =   [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                    break;
                case RSPrefsRoot_WIFI:
                    url =   [NSURL URLWithString:@"prefs:root=WIFI"];
                    break;
                case RSPrefsRoot_Bluetooth:
                    url =   [NSURL URLWithString:@"prefs:root=Bluetooth"];
                    break;
                case RSPrefsRoot_Notification:
                    url =   [NSURL URLWithString:@"prefs:root=NOTIFICATIONS_ID"];
                    break;
                case RSPrefsRoot_Wallpaper:
                    url =   [NSURL URLWithString:@"prefs:root=Wallpaper"];
                    break;
                case RSPrefsRoot_Sounds:
                    url =   [NSURL URLWithString:@"prefs:root=Sounds"];
                    break;
                    
                case RSPrefsRoot_Castle:
                    url =   [NSURL URLWithString:@"prefs:root=CASTLE"];
                    break;
                case RSPrefsRoot_Castle_StorageAndBackup:
                    url =   [NSURL URLWithString:@"prefs:root=CASTLE&path=STORAGE_AND_BACKUP"];
                    break;
                    
                case RSPrefsRoot_Store:
                    url =   [NSURL URLWithString:@"prefs:root=STORE"];
                    break;
                    
                case RSPrefsRoot_Notes:
                    url =   [NSURL URLWithString:@"prefs:root=NOTES"];
                    break;
                case RSPrefsRoot_Phone:
                    url =   [NSURL URLWithString:@"prefs:root=Phone"];
                    break;
                case RSPrefsRoot_FaceTime:
                    url =   [NSURL URLWithString:@"prefs:root=FACETIME"];
                    break;
                case RSPrefsRoot_Safari:
                    url =   [NSURL URLWithString:@"prefs:root=SAFARI"];
                    break;
                case RSPrefsRoot_Music:
                    url =   [NSURL URLWithString:@"prefs:root=MUSIC"];
                    break;
                case RSPrefsRoot_Photos:
                    url =   [NSURL URLWithString:@"prefs:root=Photos"];
                    break;
                    
                case RSPrefsRoot_Twitter:
                    url =   [NSURL URLWithString:@"prefs:root=TWITTER"];
                    break;
                case RSPrefsRoot_Facebook:
                    url =   [NSURL URLWithString:@"prefs:root=FACEBOOK"];
                    break;
                case RSPrefsRoot_Flickr:
                    url =   [NSURL URLWithString:@"prefs:root=FLICKR"];
                    break;
                case RSPrefsRoot_Vimeo:
                    url =   [NSURL URLWithString:@"prefs:root=VIMEO"];
                    break;
    // 通用
                case RSPrefsRoot_General:
                    url =   [NSURL URLWithString:@"prefs:root=General"];
                    break;
                case RSPrefsRoot_General_About:
                    url =   [NSURL URLWithString:@"prefs:root=General&path=About"];
                    break;
                case RSPrefsRoot_General_SoftwareUpdateLink:
                    url =   [NSURL URLWithString:@"prefs:root=General&path=SOFTWARE_UPDATE_LINK"];
                    break;
                case RSPrefsRoot_General_Accessibility:
                    url =   [NSURL URLWithString:@"prefs:root=General&path=ACCESSIBILITY"];
                    break;
                case RSPrefsRoot_General_Autolock:
                    url =   [NSURL URLWithString:@"prefs:root=General&path=AUTOLOCK"];
                    break;
                case RSPrefsRoot_General_DateAndTime:
                    url =   [NSURL URLWithString:@"prefs:root=General&path=DATE_AND_TIME"];
                    break;
                case RSPrefsRoot_General_Keyboard:
                    url =   [NSURL URLWithString:@"prefs:root=General&path=Keyboard"];
                    break;
                case RSPrefsRoot_General_LanguageAndRegion:
                    url =   [NSURL URLWithString:@"prefs:root=General&path=INTERNATIONAL"];
                    break;
                case RSPrefsRoot_General_VPN:
                    url =   [NSURL URLWithString:@"prefs:root=General&path=VPN"];
                    break;
                case RSPrefsRoot_General_Reset:
                    url =   [NSURL URLWithString:@"prefs:root=General&path=Reset"];
                    break;
    //隐私
                case RSPrefsRoot_Privacy:
                    url =   [NSURL URLWithString:@"prefs:root=Privacy"];
                    break;
                case RSPrefsRoot_Location_Services:
                    url =   [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
                    break;
                    
                default:
                    break;
            }
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
            [[UIApplication sharedApplication] openURL:url];
#pragma clang diagnostic pop
        }
    }else{
        url =   [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        }
    }
}

#pragma mark    当前应用版本号
+ (NSString *)applicationVersion;
{
    return [[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"];
}

#pragma mark    屏幕截图
+ (UIImage *)screenShotWithView:(UIView *)v;
{
    UIGraphicsBeginImageContext(v.frame.size);
    [v.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
































@end
